﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SLControlLibrary.Resources.Controls.Calendar.Buttons;

namespace SLControlLibrary.Resources.Controls.Calendar.ClassicCalendar
{
    public class ucHeader : Control
    {
        #region Dependency Variables
        private static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(ucHeader), new PropertyMetadata(DateTime.Today.Year.ToString()));
        private static readonly DependencyProperty ButtonLeftFillProperty = DependencyProperty.Register("ButtonLeftFill", typeof(Brush), typeof(ucHeader), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));
        private static readonly DependencyProperty ButtonRightFillProperty = DependencyProperty.Register("ButtonRightFill", typeof(Brush), typeof(ucHeader), new PropertyMetadata(new SolidColorBrush(Colors.Transparent)));
        private static readonly DependencyProperty DisplayHeightProperty = DependencyProperty.Register("DisplayHeight", typeof(double), typeof(ucHeader), null);
        private static readonly DependencyProperty DisplayWidthProperty = DependencyProperty.Register("DisplayWidth", typeof(double), typeof(ucHeader), null);
        private static readonly DependencyProperty ButtonHeightProperty = DependencyProperty.Register("ButtonHeight", typeof(double), typeof(ucHeader), new PropertyMetadata(20D));
        private static readonly DependencyProperty ButtonWidthProperty = DependencyProperty.Register("ButtonWidth", typeof(double), typeof(ucHeader), new PropertyMetadata(15D));
        public static readonly DependencyProperty AllowMonthNavigationProperty = DependencyProperty.Register("AllowMonthNavigation", typeof(bool), typeof(ucHeader), new PropertyMetadata(true, new PropertyChangedCallback(AllowMonthNavigation_Changed)));
        #endregion

        #region Dependency Properties
        public string Value
        {
            get { return (string)this.GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }
        public Brush ButtonLeftFill
        {
            get { return (Brush)this.GetValue(ButtonLeftFillProperty); }
            set { SetValue(ButtonLeftFillProperty, value); }
        }
        public Brush ButtonRightFill
        {
            get { return (Brush)this.GetValue(ButtonRightFillProperty); }
            set { SetValue(ButtonRightFillProperty, value); }
        }
        public double DisplayHeight
        {
            get { return (double)this.GetValue(DisplayHeightProperty); }
            set { SetValue(DisplayHeightProperty, value); }
        }
        public double DisplayWidth
        {
            get { return (double)this.GetValue(DisplayWidthProperty); }
            set { SetValue(DisplayWidthProperty, value); }
        }
        public double ButtonHeight
        {
            get { return (double)this.GetValue(ButtonHeightProperty); }
            set { SetValue(ButtonHeightProperty, value); }
        }
        public double ButtonWidth
        {
            get { return (double)this.GetValue(ButtonWidthProperty); }
            set { SetValue(ButtonWidthProperty, value); }
        }
        public bool AllowMonthNavigation
        {
            get { return (bool)this.GetValue(AllowMonthNavigationProperty); }
            set { this.SetValue(AllowMonthNavigationProperty, value); }
        }
        #endregion 

        #region Dependency Events
        #region AllowMonthNavigation_Changed
        private static void AllowMonthNavigation_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucHeader).OnAllowMonthNavigation_Changed(e);
        }
        #endregion
        #endregion

        #region Variables
        private ucCalendarButton btnLeft = null;
        private ucCalendarButton btnRight = null;
        private TextBlock txtDisplay = null;
        private bool IsControlLoaded = false;
        #endregion

        #region Event Variables
        public event Events.LeftClickedHandler LeftValueEvent;
        public event Events.RightClickedHandler RightValueEvent;
        public event Events.DetailsClickedHandler DetailsValueEvent;
        #endregion 

        #region Constructor
        public ucHeader()
        {
            DefaultStyleKey = typeof(ucHeader);
        }
        #endregion

        #region Methods
        #region OnApplyTemplate
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            btnLeft = (ucCalendarButton)base.GetTemplateChild("btnLeft");
            btnRight = (ucCalendarButton)base.GetTemplateChild("btnRight");
            txtDisplay = (TextBlock)base.GetTemplateChild("txtDisplay");

            if (btnLeft == null) return;
            if (btnRight == null) return;
            if (txtDisplay == null) return;

            btnLeft.Click += BtnLeft_Click;
            btnRight.Click += BtnRight_Click;
            txtDisplay.MouseLeftButtonUp += TxtDisplay_MouseLeftButtonUp;

            IsControlLoaded = true;
        }        
        #endregion

        #region OnAllowMonthNavigation_Changed
        protected virtual void OnAllowMonthNavigation_Changed(DependencyPropertyChangedEventArgs e)
        {
            AllowMonthNavigation = (bool)e.NewValue;

            if (IsControlLoaded)
            {
                if (AllowMonthNavigation)
                {
                    btnLeft.Cursor = Cursors.Hand;
                    btnRight.Cursor = Cursors.Hand;
                    txtDisplay.Cursor = Cursors.Hand;
                    btnLeft.Visibility = Visibility.Visible;
                    btnRight.Visibility = Visibility.Visible;

                    btnLeft.Click += BtnLeft_Click;
                    btnRight.Click += BtnRight_Click;
                    txtDisplay.MouseLeftButtonUp += TxtDisplay_MouseLeftButtonUp;
                }
                else
                {
                    btnLeft.Cursor = Cursors.Arrow;
                    btnRight.Cursor = Cursors.Arrow;
                    txtDisplay.Cursor = Cursors.Arrow;
                    btnLeft.Visibility = Visibility.Collapsed;
                    btnRight.Visibility = Visibility.Collapsed;

                    btnLeft.Click -= BtnLeft_Click;
                    btnRight.Click -= BtnRight_Click;
                    txtDisplay.MouseLeftButtonUp -= TxtDisplay_MouseLeftButtonUp;
                }
            }
        }
        #endregion
        #endregion

        #region Events
        #region BtnLeft_Click
        private void BtnLeft_Click(object sender, RoutedEventArgs e)
        {
            if (LeftValueEvent != null)
                LeftValueEvent(this, null);
        }
        #endregion

        #region BtnRight_Click
        private void BtnRight_Click(object sender, RoutedEventArgs e)
        {
            if (RightValueEvent != null)
                RightValueEvent(this, null);
        }
        #endregion

        #region TxtDisplay_MouseLeftButtonUp
        private void TxtDisplay_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (DetailsValueEvent != null)
                DetailsValueEvent(this, null);
        }
        #endregion
        #endregion 
    }
}
